package com.bdqn.controller;




import java.util.Calendar;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bdqn.dto.Dto;
import com.bdqn.model.KeyWord;
import com.bdqn.model.Users;
import com.bdqn.service.KeyWordService;
import com.github.pagehelper.PageInfo;


@Controller
@RequestMapping("/keyWord")
public class KeyWordController {

	@Autowired
	private KeyWordService keyWordService;
	//精确查询
	@RequestMapping("/kwSearch")
	@ResponseBody
	public int select(Model model,@RequestParam("keyWord") String KeyWord){
			
		int num= keyWordService.selectKeyWord(KeyWord);

		System.out.println(num);
		
		return num;	
	}
	//模糊查询
	@RequestMapping("/likeSearch")
	public String likeSearch(Model model,@RequestParam("keyWord") String KeyWord){
			
		PageInfo<KeyWord> kd = new PageInfo<>();
		
		boolean isExist = true;
		
		if(KeyWord==""||KeyWord==null) {
			kd = keyWordService.selectAll();
		}else {
			kd = keyWordService.selectLikeKeyWord(KeyWord);
			 
			isExist = keyWordService.selectByKeyWord(KeyWord);
		}
		model.addAttribute("kd", kd);
		
		model.addAttribute("searchName", KeyWord);
		
		if(!isExist) {
			model.addAttribute("msg", "关键词不存在请重新搜索！");
		}
		
		return "keyWordManage";			
	}
	
	//分页查询
	@RequestMapping("/page")
	public String page(Model model,Dto dto){
			
		PageInfo<KeyWord> kd = keyWordService.page(dto);
		
		model.addAttribute("kd", kd);
		
		model.addAttribute("searchName", dto.getWord());

		return "keyWordManage";			
	}
	
	@RequestMapping("/kwInsert/{agent}")
	@ResponseBody
	public int insert(@PathVariable("agent")String agent, KeyWord kw){
		
		Date date = new Date();
		
		Users user = new Users();
		
		user.setLoginName(agent);
		
		int year = kw.getYear();
		
		kw.setStartDate(date);
		
		Calendar rightNow = Calendar.getInstance();
		
        rightNow.setTime(date);
        
        rightNow.add(Calendar.YEAR,year);//日期加year年
        
        Date endDate=rightNow.getTime();

		System.out.println(date);
        System.out.println(endDate);
		
		kw.setEndDate(endDate);
		
		kw.setUser(user);
		
		int num = keyWordService.insert(kw);
		
		return num;
		
	}
	

}
